package huxibianjie.com.lbscandroid.ui.new_activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;

/**
 * 补全密码
 */
public class CompleteCipherActivity extends BaseActivity implements View.OnClickListener {

    /**
     * 请输入密码
     */
    private EditText etUserPassword;
    /**
     * 确定
     */
    private Button btnCipher;


    @Override
    protected int getContentView() {
        return R.layout.activity_completecipher;
    }


    protected void initView() {
        etUserPassword = findViewById(R.id.et_user_password);
        btnCipher = findViewById(R.id.btn_cipher);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        btnCipher.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = getIntent();
        String phone = intent.getStringExtra("phone");
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_cipher:
                String password = etUserPassword.getText().toString();
                if (!isLetterDigit(password)) {
                    showToast("密码格式不正确，请重新输入。");
                    etUserPassword.setText("");
                    return;
                }
                Task.create(Service.class).completionPassword(phone, password).enqueue(new huxibianjie.com.lbscandroid.task.Callback<UserModel>() {
                    @Override
                    public void onSuccess(@Nullable UserModel userModel) {
                        if (userModel != null) {
                            UserManager.getInstance().save(userModel);
                            showToast("登录成功");
                            finish();
                        } else {
                            showToast("登录失败");
                        }
                    }

                    @Override
                    public void onFailure(@NonNull String error) {
                        showToast(error);
                    }
                });
        }
    }

    /**
     * 规则：至少包含大小写字母及数字中的两种
     */
    public static boolean isLetterDigit(String str) {
        return str.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
    }
}